package com.mjf.cjt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.cjt.domain.TFExpenseInfo;
import com.mjf.cjt.mapper.TFExpenseInfoMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.TFExpenseInfoService;
import com.mjf.cjt.vo.ExpenseListVO;
import com.mjf.cjt.vo.ExpenseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/5/23 11:37
 */
@Service
public class TFExpenseInfoServiceImpl extends ServiceImpl<TFExpenseInfoMapper, TFExpenseInfo> implements TFExpenseInfoService {


    @Autowired
    private AsyncTMService asyncTMService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncFYDA() {
        List<ExpenseVO> expenseVOS = asyncTMService.asyncFYDA();
        if (CollectionUtil.isNotEmpty(expenseVOS)) {
            List<TFExpenseInfo> expenseInfos = CollectionUtil.newArrayList();
            for (ExpenseVO expenseVO : expenseVOS) {
                TFExpenseInfo expenseInfo = new TFExpenseInfo();
                expenseInfo.setId(expenseVO.getId());
                expenseInfo.setCode(expenseVO.getCode());
                expenseInfo.setName(expenseVO.getName());
                if (expenseVO.getParent() != null) {
                    expenseInfo.setParentId(expenseVO.getParent().getId());
                }
                expenseInfo.setExpenseTypeCode(expenseVO.getExpenseType().getCode());
                expenseInfo.setExpenseTypeId(expenseVO.getExpenseType().getId());
                expenseInfo.setExpenseTypeName(expenseVO.getExpenseType().getName());
                expenseInfos.add(expenseInfo);
            }
            saveOrUpdateBatch(expenseInfos);
        }
    }

    @Override
    public List<ExpenseListVO> expenseList() {
        List<TFExpenseInfo> expenseInfos = list(Wrappers.<TFExpenseInfo>lambdaQuery().orderByAsc(TFExpenseInfo::getId));
        List<ExpenseListVO> vos = expenseInfos.stream().map(e -> ExpenseListVO.builder()
            .id(e.getId())
            .code(e.getCode())
            .name(e.getName())
            .parentId(e.getParentId())
            .build()).collect(Collectors.toList());
        return getExpenseInfos(vos);
    }

    /**
     * 转换层级结构
     * @param vos
     * @return
     */
    private List<ExpenseListVO> getExpenseInfos(List<ExpenseListVO> vos) {
        List<ExpenseListVO> l1 = vos.stream().filter(e -> e.getParentId() == null).collect(Collectors.toList());
        for (ExpenseListVO expenseListVO : l1) {
            List<ExpenseListVO> l2 = vos.stream().filter(e -> expenseListVO.getId().equals(e.getParentId())).collect(Collectors.toList());
            expenseListVO.setChildren(l2);
        }
        return l1;
    }

}
